package sk.project.accounting.data.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sk.project.accounting.data.model.User;

@Repository
public class UserDaoImpl implements UserDao{
	
	private static Logger log = Logger.getLogger(UserDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
		
	@Override
	@SuppressWarnings("unchecked")
	public List<User> getAllUsers() {
		log.info("Get all users");
		Session session = sessionFactory.getCurrentSession();	
		Criteria criteria = session.createCriteria(User.class);
		List<User> users = criteria.list();
		return users;
	}

	@Override
	public User getUserById(int id) {
		log.info("Get user by Id: " + id);
		Session session = sessionFactory.getCurrentSession();
		User user = (User)session.get(User.class, id);
		return user;
	}

	@Override
	public int insertUser(User user) {
		log.info("Insert user: " + user.toString());
		Session session = sessionFactory.getCurrentSession();
		int id = (Integer)session.save(user);
		return id;
	}
		
	public void deleteUser(User user) {
		log.info("Remove user: " + user.toString());
		Session session = sessionFactory.getCurrentSession();
		session.delete(user);
	}

}
